<?php
/**
 * Created by PhpStorm.
 * User: Celebration-Grey
 * Date: 2017/5/20
 * Time: 19:32
 */

namespace App\Http\Classes;


use Illuminate\Support\Facades\DB;

class FriendGroup
{
    private $group_id;
    private $user_id;
    private $db;
    private $info;

    public function __construct($group_id,$user_id)
    {
        $this -> group_id = $group_id;
        $this -> user_id = $user_id;
        $this -> db = DB::table('weibo_friend_groups');
    }

    private function selectInfo(){
        $this->info = $this->db -> where('group_id','=',$this->group_id)
            -> where('group_user_id','=',$this->user_id);
    }

    public function updateGroupName($name){
        $this->selectInfo();
        $result = $this -> info -> update([
            'group_name' => $name
        ]);
        if($result){
            return true;
        }
        else{
            return false;
        }
    }

    public static function staticGroupCount($userId){
        $count = DB::table('weibo_friend_groups') -> where('group_user_id','=',$userId)->count();
        return (int)$count;
    }

    public function groupCount(){
        $count = $this -> db -> where('group_user_id','=',$this->user_id)->count();
        return (int)$count;
    }

    public function deleteGroup(){
        $this->selectInfo();
        if($this->groupCount() >= 2){
            $result = $this -> info -> delete();
            if($result){
                return true;
            }
            else{
                return false;
            }
        }
        else{
            return null;
        }
    }

    public static function isGroupNameExists($data){
        if(isset($data['group_name']) && isset($data['group_user_id'])){
            $group = DB::table('weibo_friend_groups');
            $result = $group -> where('group_user_id','=',$data['group_user_id']) -> where('group_name','=',$data['group_name'])-> count();
            if($result >= 1){
                return true;
            }
            else{
                return false;
            }
        }
    }

    public static function addGroup($data){
        if(isset($data['group_name']) && isset($data['group_user_id'])){
            $group = DB::table('weibo_friend_groups');
            $result = $group->insertGetId([
                'group_name' => $data['group_name'],
                'group_user_id' => $data['group_user_id']
            ]);
            if($result){
                $result = $group -> where('group_id','=',$result)->get();
                if($result){
                    return $result;
                }
                else{
                    return null;
                }
            }
            else{
                return false;
            }
        }
        else{
            return false;
        }
    }

    public static function addDeafultGroup($userId){
        if(self::staticGroupCount($userId) <= 0){
            $group = DB::table('weibo_friend_groups');
            $result = $group->insertGetId([
                'group_name' => '未分组',
                'group_user_id' => $userId
            ]);
            if($result){
                return $result;
            }
            else{
                return null;
            }
        }
        else{
            return false;
        }
    }

    public static function getUserAllGroups($userId){
        $group = DB::table('weibo_friend_groups');
        $group = $group->where('group_user_id','=',$userId)->get();
        if($group){
            return $group;
        }
        else{
            return null;
        }
    }

    public static function groupMemberTransfer(){

    }
}